<template>
	<view class="">
		<common-title :title="titleContent" @clickLeft="backToUpPage"></common-title>
		<restaurant-order-list
			:orderedInfo="orderedInfo"
			:currentUserId="currentUserId"
			:operationButton="operationButton"
			@click-order-details="clickOrderDetails"
			@cancel-order="cancelOrder"
			@received-order="receivedOrder"
		></restaurant-order-list>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { apiRestaurantOrdersByStatus, apiRestaurantEditOrderStatus } from '../../../utils/apiRestaurant';
	import { getDataFromStorage } from '../../../utils/commonUtils';
	//==============================标题===============================
	//标题内容
	const titleContent = ref({
		left:'返回',
		midst:'餐厅-已完成订单'
	});
	//返回
	const backToUpPage = () => {
		uni.switchTab({
			url:'/pages/message/message'
		});
	};
	//-------------------------------订单组件-------------------------------------
	// 定义数据
	const orderedInfo = ref([]); // 要从 API 获取的订单信息
	const currentUserId = ref(0); // 当前用户的 ID
	const operationButton = ref({
		opera1:"取消订单",
		opera2:"完成"
	})
	// 获取数据的生命周期钩子
	onLoad(() => {
		apiRestaurantOrdersByStatus(3).then((res) => {
			orderedInfo.value = [...orderedInfo.value, ...res];
			// console.log(orderedInfo.value);
		});
		apiRestaurantOrdersByStatus(4).then((res) => {
			orderedInfo.value = [...orderedInfo.value, ...res];
			// console.log(orderedInfo.value);
		});
		getDataFromStorage('userId').then((res) => {
			currentUserId.value = res ? Number(res) : 0; // 确保是数字，设置默认值为0
			// console.log(currentUserId.value)
			// console.log(typeof(currentUserId.value));
		});
	})
	
	//-----------------------------订单细节-------------------------------------
	const clickOrderDetails = (orderNo) => {
		uni.navigateTo({
			url:`/pages/index/restaurant/restaurant-checkout?orderNo=${encodeURIComponent(orderNo)}`
		})
	}
	//-----------------------------取消订单-------------------------------------
	const cancelOrder = (orderNo) => {
	    console.log('Cancel order:', orderNo);
	};
	
	//-----------------------------确认订单-------------------------------------
	const receivedOrder = (orderNo) => {
	    console.log('Received order:', orderNo);
	};
</script>

<style lang="scss" scoped>

</style>